package com.yu.juc;

import java.util.concurrent.Semaphore;

/**
 * @Author Irelia
 * @Date 2024/7/5 12:10
 */
public class SemaphoreDemoTwo {

    /**
     * release 会让Semaphore为0的获取一共信号量
     * acquire 会阻塞在这里  直到获取到信号量
     */
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(0);

        Thread thread = new Thread(() -> {
            try {
                System.out.println("before");
                //TimeUnit.SECONDS.sleep(3);
                semaphore.acquire(); // 阻塞
                System.out.println("after");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        Thread thread1 = new Thread(() -> {
            //semaphore.release();
            System.out.println("释放");
        });
        thread.start();
        thread1.start();
    }
}
